#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA
@Software: PyCharm 、Qt Designer and PySide6
@File: tQMainWindowWidget.py
@Time: 2024/04/03 13:32
@Function：ctQMainWindowWidget：MainWindow布局（ctQMainWindowUI）中添加控件
"""
from UI.cores.tFunctions import Functions
from tQQtCore import *
from UI.cores.tJsonSettings import Settings
from UI.cores.tJsonThemes import Themes
from UI.widgets import *
from .tQMainWindowFunc import ctQMainWindowFunc
from .tQMainWindowFunc import *


# MainWindow布局中添加控件
class ctQMainWindowWidget:
    def __init__(self):
        super().__init__()
        # MainWindow GUI 界面实现
        self.ui = ctQMainWindowUI()
        # 调用布局函数
        self.ui.f_mainFormLayout(self)

    # <editor-fold desc="添加左侧菜单栏按键、顶部标题栏按键">
    # 添加左侧菜单栏按键
    btnLst = [
        # 主页
        {
            "btn_icon": "icon_home.svg",
            "btn_id": "btn_home",
            "btn_text": "主页",
            "btn_tooltip": "主页",
            "show_top": True,
            "is_active": True
        },
        # 添加用户
        {
            "btn_icon": "icon_add_user.svg",
            "btn_id": "btn_add_user",
            "btn_text": "添加用户",
            "btn_tooltip": "添加用户",
            "show_top": True,
            "is_active": False
        },
        # 部件展示
        {
            "btn_icon": "icon_widgets.svg",
            "btn_id": "btn_widgets",
            "btn_text": "显示自定义部件",
            "btn_tooltip": "显示自定义部件",
            "show_top": True,
            "is_active": False
        },
        # {
        #     "btn_icon" : "icon_add_user.svg",
        #     "btn_id" : "btn_add_user",
        #     "btn_text" : "Add Users",
        #     "btn_tooltip" : "Add users",
        #     "show_top" : True,
        #     "is_active" : False
        # },
        # {
        #     "btn_icon" : "icon_file.svg",
        #     "btn_id" : "btn_new_file",
        #     "btn_text" : "New File",
        #     "btn_tooltip" : "Create new file",
        #     "show_top" : True,
        #     "is_active" : False
        # },
        # {
        #     "btn_icon" : "icon_folder_open.svg",
        #     "btn_id" : "btn_open_file",
        #     "btn_text" : "Open File",
        #     "btn_tooltip" : "Open file",
        #     "show_top" : True,
        #     "is_active" : False
        # },
        # {
        #     "btn_icon" : "icon_save.svg",
        #     "btn_id" : "btn_save",
        #     "btn_text" : "Save File",
        #     "btn_tooltip" : "Save file",
        #     "show_top" : True,
        #     "is_active" : False
        # },
        # 信息
        {
            "btn_icon": "icon_info.svg",
            "btn_id": "btn_info",
            "btn_text": "信息",
            "btn_tooltip": "打开信息",
            "show_top": False,
            "is_active": False
        },
        # 设置
        {
            "btn_icon": "icon_settings.svg",
            "btn_id": "btn_settings",
            "btn_text": "设置",
            "btn_tooltip": "打开设置",
            "show_top": False,
            "is_active": False
        }
    ]

    # 添加顶部标题栏按键
    titlebarMenuLst = [
        # 搜索
        {
            "btn_icon": "icon_search.svg",
            "btn_id": "btn_search",
            "btn_tooltip": "搜索",
            "is_active": False
        },
        # 设置
        {
            "btn_icon": "icon_settings.svg",
            "btn_id": "btn_top_settings",
            "btn_tooltip": "顶部设置",
            "is_active": False
        }
    ]

    # </editor-fold>

    # 主窗体各控件布局函数
    def f_mainFormWidgetLayout(self):
        # 设置标题
        self.setWindowTitle(self.settings["app_name"])

        # 移除自带的标题栏
        if self.settings["custom_title_bar"]:
            self.setWindowFlag(Qt.FramelessWindowHint)
            self.setAttribute(Qt.WA_TranslucentBackground)

        # 如果使用自定义标题栏，添加手柄
        if self.settings["custom_title_bar"]:
            self.left_grip = ctQGrips(self, "left", self.hide_grips)
            self.right_grip = ctQGrips(self, "right", self.hide_grips)
            self.top_grip = ctQGrips(self, "top", self.hide_grips)
            self.bottom_grip = ctQGrips(self, "bottom", self.hide_grips)
            self.top_left_grip = ctQGrips(self, "top_left", self.hide_grips)
            self.top_right_grip = ctQGrips(self, "top_right", self.hide_grips)
            self.bottom_left_grip = ctQGrips(self, "bottom_left", self.hide_grips)
            self.bottom_right_grip = ctQGrips(self, "bottom_right", self.hide_grips)

        # <editor-fold desc="左侧菜单栏">
        # 左侧菜单栏添加按键
        self.ui.leftMenuBar.f_leftMenuAddButtons(ctQMainWindowWidget.btnLst)

        # 左侧菜单栏按键设置信号
        self.ui.leftMenuBar.clicked.connect(self.f_titleBarAndLeftColumnBtnClicked)
        self.ui.leftMenuBar.released.connect(self.f_titleBarAndLeftColumnBtnReleased)
        # </editor-fold>

        # <editor-fold desc="标题栏">
        # 标题栏按键设置信号
        self.ui.titleBar.clicked.connect(self.f_titleBarAndLeftColumnBtnClicked)
        self.ui.titleBar.released.connect(self.f_titleBarAndLeftColumnBtnReleased)

        # 标题栏添加按键
        self.ui.titleBar.f_titleBarAddMenus(ctQMainWindowWidget.titlebarMenuLst)

        # 标题栏设置标题
        if self.settings["custom_title_bar"]:
            self.ui.titleBar.f_setTitle(self.settings["app_name"])
        else:
            self.ui.titleBar.f_setTitle("Welcome to QDarkMordenGUI")
        # </editor-fold>

        # 左列按键设置信号
        self.ui.leftColumn.leftColumnBtnClicked.connect(self.f_titleBarAndLeftColumnBtnClicked)
        self.ui.leftColumn.leftColumnBtnReleased.connect(self.f_titleBarAndLeftColumnBtnReleased)

        # 设置页面 & 左右列页面
        ctQMainWindowFunc.f_setPageFunc(self, self.ui.mainPages.page_1)
        ctQMainWindowFunc.f_setLeftColumnMenu(self,
                                              menu=self.ui.leftColumn.menus.menu_1,
                                              title="Settings Left Column",
                                              icon_path=Functions.f_getSvgIconPath("icon_settings.svg")
                                              )
        ctQMainWindowFunc.f_setRightColumnMenu(self, self.ui.rightColumn.menu_1)

        # <editor-fold desc="加载设置 & 主题">
        # 加载设置
        settings = Settings()
        self.settings = settings.items

        # 加载主题
        themes = Themes()
        self.themes = themes.items
        # </editor-fold>

        # <editor-fold desc="左列">
        # <editor-fold desc="按键">
        # BTN 1
        self.btnLeftOne = ctQPushButton(text="按键",
                                        radius=8,
                                        color=self.themes["app_color"]["text_foreground"],
                                        bg_color=self.themes["app_color"]["dark_one"],
                                        bg_color_hover=self.themes["app_color"]["dark_three"],
                                        bg_color_pressed=self.themes["app_color"]["dark_four"]
                                        )
        self.btnLeftOne.setMaximumHeight(40)
        self.ui.leftColumn.menus.btn_1_layout.addWidget(self.btnLeftOne)

        # BTN 2
        self.btnLeftTwo = ctQPushButton(text="按键显示图标",
                                        radius=8,
                                        color=self.themes["app_color"]["text_foreground"],
                                        bg_color=self.themes["app_color"]["dark_one"],
                                        bg_color_hover=self.themes["app_color"]["dark_three"],
                                        bg_color_pressed=self.themes["app_color"]["dark_four"]
                                        )
        self.iconLeftButton = QIcon(Functions.f_getSvgIconPath("icon_settings.svg"))
        self.btnLeftTwo.setIcon(self.iconLeftButton)
        self.btnLeftTwo.setMaximumHeight(40)
        self.ui.leftColumn.menus.btn_2_layout.addWidget(self.btnLeftTwo)

        # BTN 3 - Default QPushButton
        self.btnLeftThree = QPushButton("默认的QPushButton")
        self.btnLeftThree.setMaximumHeight(40)
        self.ui.leftColumn.menus.btn_3_layout.addWidget(self.btnLeftThree)
        # </editor-fold>
        # </editor-fold>

        # <editor-fold desc="PAGE 1">
        self.logoSvg = QSvgWidget(Functions.f_getSvgImagePath("cosmo.svg"))
        self.ui.mainPages.logo_layout.addWidget(self.logoSvg, Qt.AlignCenter, Qt.AlignCenter)
        # </editor-fold>

        # <editor-fold desc="PAGE 2">
        # <editor-fold desc="自定义圆形进度条">
        # CIRCULAR PROGRESS 1
        self.circularPprogressOne = ctQCircularProgress(value=80,
                                                        progress_color=self.themes["app_color"]["context_color"],
                                                        text_color=self.themes["app_color"]["text_title"],
                                                        font_size=14,
                                                        bg_color=self.themes["app_color"]["dark_four"]
                                                        )
        self.circularPprogressOne.setFixedSize(200, 200)

        # CIRCULAR PROGRESS 2
        self.circularPprogressTwo = ctQCircularProgress(value=45,
                                                        progress_width=4,
                                                        progress_color=self.themes["app_color"]["context_color"],
                                                        text_color=self.themes["app_color"]["context_color"],
                                                        font_size=14,
                                                        bg_color=self.themes["app_color"]["bg_three"]
                                                        )
        self.circularPprogressTwo.setFixedSize(160, 160)

        # CIRCULAR PROGRESS 3
        self.circularPprogressThree = ctQCircularProgress(value=75,
                                                          progress_width=2,
                                                          progress_color=self.themes["app_color"]["pink"],
                                                          text_color=self.themes["app_color"]["white"],
                                                          font_size=14,
                                                          bg_color=self.themes["app_color"]["bg_three"]
                                                          )
        self.circularPprogressThree.setFixedSize(140, 140)
        # </editor-fold>

        # <editor-fold desc="滑块控件">
        # PY SLIDER 1
        self.sliderOne = ctQSlider(margin=8,
                                   bg_size=10,
                                   bg_radius=5,
                                   handle_margin=-3,
                                   handle_size=16,
                                   handle_radius=8,
                                   bg_color=self.themes["app_color"]["dark_three"],
                                   bg_color_hover=self.themes["app_color"]["dark_four"],
                                   handle_color=self.themes["app_color"]["context_color"],
                                   handle_color_hover=self.themes["app_color"]["context_hover"],
                                   handle_color_pressed=self.themes["app_color"]["context_pressed"]
                                   )
        self.sliderOne.setMinimumHeight(100)

        # PY SLIDER 2
        self.sliderTwo = ctQSlider(bg_color=self.themes["app_color"]["dark_three"],
                                   bg_color_hover=self.themes["app_color"]["dark_three"],
                                   handle_color=self.themes["app_color"]["context_color"],
                                   handle_color_hover=self.themes["app_color"]["context_hover"],
                                   handle_color_pressed=self.themes["app_color"]["context_pressed"]
                                   )
        self.sliderTwo.setMinimumHeight(100)

        # PY SLIDER 3
        self.sliderThree = ctQSlider(margin=8,
                                     bg_size=10,
                                     bg_radius=5,
                                     handle_margin=-3,
                                     handle_size=16,
                                     handle_radius=8,
                                     bg_color=self.themes["app_color"]["dark_three"],
                                     bg_color_hover=self.themes["app_color"]["dark_four"],
                                     handle_color=self.themes["app_color"]["context_color"],
                                     handle_color_hover=self.themes["app_color"]["context_hover"],
                                     handle_color_pressed=self.themes["app_color"]["context_pressed"]
                                     )
        self.sliderThree.setOrientation(Qt.Horizontal)
        self.sliderThree.setMaximumWidth(200)

        # PY SLIDER 4
        self.sliderFour = ctQSlider(
            bg_color=self.themes["app_color"]["dark_three"],
            bg_color_hover=self.themes["app_color"]["dark_three"],
            handle_color=self.themes["app_color"]["context_color"],
            handle_color_hover=self.themes["app_color"]["context_hover"],
            handle_color_pressed=self.themes["app_color"]["context_pressed"]
        )
        self.sliderFour.setOrientation(Qt.Horizontal)
        self.sliderFour.setMaximumWidth(200)
        # </editor-fold>

        # <editor-fold desc="图标按键">
        self.btnIconHeart = ctQIconButton(icon_path=Functions.f_getSvgIconPath("icon_heart.svg"),
                                          parent=self,
                                          app_parent=self.ui.widgetCentral,
                                          tooltip_text="Icon button - Heart",
                                          width=40,
                                          height=40,
                                          radius=20,
                                          dark_one=self.themes["app_color"]["dark_one"],
                                          icon_color=self.themes["app_color"]["icon_color"],
                                          icon_color_hover=self.themes["app_color"]["icon_hover"],
                                          icon_color_pressed=self.themes["app_color"]["icon_active"],
                                          icon_color_active=self.themes["app_color"]["icon_active"],
                                          bg_color=self.themes["app_color"]["dark_one"],
                                          bg_color_hover=self.themes["app_color"]["dark_three"],
                                          bg_color_pressed=self.themes["app_color"]["pink"]
                                          )

        self.btnIconAddUser = ctQIconButton(icon_path=Functions.f_getSvgIconPath("icon_add_user.svg"),
                                            parent=self,
                                            app_parent=self.ui.widgetCentral,
                                            tooltip_text="BTN with tooltip",
                                            width=40,
                                            height=40,
                                            radius=8,
                                            dark_one=self.themes["app_color"]["dark_one"],
                                            icon_color=self.themes["app_color"]["icon_color"],
                                            icon_color_hover=self.themes["app_color"]["icon_hover"],
                                            icon_color_pressed=self.themes["app_color"]["white"],
                                            icon_color_active=self.themes["app_color"]["icon_active"],
                                            bg_color=self.themes["app_color"]["dark_one"],
                                            bg_color_hover=self.themes["app_color"]["dark_three"],
                                            bg_color_pressed=self.themes["app_color"]["green"],

                                            )

        self.btnIconAddUserActive = ctQIconButton(icon_path=Functions.f_getSvgIconPath("icon_add_user.svg"),
                                                  parent=self,
                                                  app_parent=self.ui.widgetCentral,
                                                  tooltip_text="BTN actived! (is_actived = True)",
                                                  width=40,
                                                  height=40,
                                                  radius=8,
                                                  dark_one=self.themes["app_color"]["dark_one"],
                                                  icon_color=self.themes["app_color"]["icon_color"],
                                                  icon_color_hover=self.themes["app_color"]["icon_hover"],
                                                  icon_color_pressed=self.themes["app_color"]["white"],
                                                  icon_color_active=self.themes["app_color"]["icon_active"],
                                                  bg_color=self.themes["app_color"]["dark_one"],
                                                  bg_color_hover=self.themes["app_color"]["dark_three"],
                                                  bg_color_pressed=self.themes["app_color"]["context_color"],
                                                  is_active=True
                                                  )
        # </editor-fold>

        # <editor-fold desc="按键">
        # PUSH BUTTON 1
        self.btnOne = ctQPushButton(text="按键",
                                    radius=8,
                                    color=self.themes["app_color"]["text_foreground"],
                                    bg_color=self.themes["app_color"]["dark_one"],
                                    bg_color_hover=self.themes["app_color"]["dark_three"],
                                    bg_color_pressed=self.themes["app_color"]["dark_four"]
                                    )
        self.btnOne.setMinimumHeight(40)

        # PUSH BUTTON 2
        self.btnWithIcon = ctQPushButton(text="带有Icon的按键",
                                         radius=8,
                                         color=self.themes["app_color"]["text_foreground"],
                                         bg_color=self.themes["app_color"]["dark_one"],
                                         bg_color_hover=self.themes["app_color"]["dark_three"],
                                         bg_color_pressed=self.themes["app_color"]["dark_four"]
                                         )
        self.icon_2 = QIcon(Functions.f_getSvgIconPath("icon_settings.svg"))
        self.btnWithIcon.setMinimumHeight(40)
        self.btnWithIcon.setIcon(self.icon_2)
        # </editor-fold>

        # <editor-fold desc="文本框">
        # 文本框
        self.edit = ctQLineEdit(text="文本框",
                                place_holder_text="Place holder text",
                                radius=8,
                                border_size=2,
                                color=self.themes["app_color"]["text_foreground"],
                                selection_color=self.themes["app_color"]["white"],
                                bg_color=self.themes["app_color"]["dark_one"],
                                bg_color_active=self.themes["app_color"]["dark_three"],
                                context_color=self.themes["app_color"]["context_color"]
                                )
        self.edit.setMinimumHeight(30)
        # </editor-fold>

        # <editor-fold desc="切换开关">
        self.tog = ctQToggleSwitch(width=50,
                                   bg_color=self.themes["app_color"]["dark_two"],
                                   circle_color=self.themes["app_color"]["icon_color"],
                                   active_color=self.themes["app_color"]["context_color"]
                                   )
        # </editor-fold>

        # <editor-fold desc="表格">
        # 表格控件
        self.table = ctQTableWidget(radius=8,
                                    color=self.themes["app_color"]["text_foreground"],
                                    selection_color=self.themes["app_color"]["context_color"],
                                    bg_color=self.themes["app_color"]["bg_two"],
                                    header_horizontal_color=self.themes["app_color"]["dark_two"],
                                    header_vertical_color=self.themes["app_color"]["bg_three"],
                                    bottom_line_color=self.themes["app_color"]["bg_three"],
                                    grid_line_color=self.themes["app_color"]["bg_one"],
                                    scroll_bar_bg_color=self.themes["app_color"]["bg_one"],
                                    scroll_bar_btn_color=self.themes["app_color"]["dark_four"],
                                    context_color=self.themes["app_color"]["context_color"]
                                    )
        self.table.setColumnCount(3)
        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.setSelectionMode(QAbstractItemView.ExtendedSelection)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)

        # Columns / Header
        self.tableColumnOne = QTableWidgetItem()
        self.tableColumnOne.setTextAlignment(Qt.AlignCenter)
        self.tableColumnOne.setText("NAME")

        self.tableColumnTwo = QTableWidgetItem()
        self.tableColumnTwo.setTextAlignment(Qt.AlignCenter)
        self.tableColumnTwo.setText("NICK")

        self.tableColumnThree = QTableWidgetItem()
        self.tableColumnThree.setTextAlignment(Qt.AlignCenter)
        self.tableColumnThree.setText("PASS")

        # Set column
        self.table.setHorizontalHeaderItem(0, self.tableColumnOne)
        self.table.setHorizontalHeaderItem(1, self.tableColumnTwo)
        self.table.setHorizontalHeaderItem(2, self.tableColumnThree)

        for x in range(10):
            row_number = self.table.rowCount()
            self.table.insertRow(row_number)  # Insert row
            self.table.setItem(row_number, 0, QTableWidgetItem(str("Wanderson")))  # Add name
            self.table.setItem(row_number, 1, QTableWidgetItem(str("vfx_on_fire_" + str(x))))  # Add nick
            self.passText = QTableWidgetItem()
            self.passText.setTextAlignment(Qt.AlignCenter)
            self.passText.setText("12345" + str(x))
            self.table.setItem(row_number, 2, self.passText)  # Add pass
            self.table.setRowHeight(row_number, 22)
        # </editor-fold>

        # ADD WIDGETS
        self.ui.mainPages.row_1_layout.addWidget(self.circularPprogressOne)
        self.ui.mainPages.row_1_layout.addWidget(self.circularPprogressTwo)
        self.ui.mainPages.row_1_layout.addWidget(self.circularPprogressThree)
        self.ui.mainPages.row_2_layout.addWidget(self.sliderOne)
        self.ui.mainPages.row_2_layout.addWidget(self.sliderTwo)
        self.ui.mainPages.row_2_layout.addWidget(self.sliderThree)
        self.ui.mainPages.row_2_layout.addWidget(self.sliderFour)
        self.ui.mainPages.row_3_layout.addWidget(self.btnIconHeart)
        self.ui.mainPages.row_3_layout.addWidget(self.btnIconAddUser)
        self.ui.mainPages.row_3_layout.addWidget(self.btnIconAddUserActive)
        self.ui.mainPages.row_3_layout.addWidget(self.btnOne)
        self.ui.mainPages.row_3_layout.addWidget(self.btnWithIcon)
        self.ui.mainPages.row_3_layout.addWidget(self.tog)
        self.ui.mainPages.row_4_layout.addWidget(self.edit)
        self.ui.mainPages.row_5_layout.addWidget(self.table)
        # </editor-fold>

        # <editor-fold desc="右侧列">
        self.btnRightOne = ctQPushButton(text="展示第二页",
                                         radius=8,
                                         color=self.themes["app_color"]["text_foreground"],
                                         bg_color=self.themes["app_color"]["dark_one"],
                                         bg_color_hover=self.themes["app_color"]["dark_three"],
                                         bg_color_pressed=self.themes["app_color"]["dark_four"]
                                         )
        self.iconRight = QIcon(Functions.f_getSvgIconPath("icon_arrow_right.svg"))
        self.btnRightOne.setIcon(self.iconRight)
        self.btnRightOne.setMaximumHeight(40)
        self.btnRightOne.clicked.connect(lambda: ctQMainWindowFunc.f_setRightColumnMenu(self,
                                                                                        self.ui.rightColumn.menu_2
                                                                                        ))
        self.ui.rightColumn.btn_1_layout.addWidget(self.btnRightOne)

        # BTN 2
        self.btnRightTwo = ctQPushButton(text="展示第一页",
                                         radius=8,
                                         color=self.themes["app_color"]["text_foreground"],
                                         bg_color=self.themes["app_color"]["dark_one"],
                                         bg_color_hover=self.themes["app_color"]["dark_three"],
                                         bg_color_pressed=self.themes["app_color"]["dark_four"]
                                         )
        self.iconLeft = QIcon(Functions.f_getSvgIconPath("icon_arrow_left.svg"))
        self.btnRightTwo.setIcon(self.iconLeft)
        self.btnRightTwo.setMaximumHeight(40)
        self.btnRightTwo.clicked.connect(lambda: ctQMainWindowFunc.f_setRightColumnMenu(self,
                                                                                        self.ui.rightColumn.menu_1
                                                                                        ))
        self.ui.rightColumn.btn_2_layout.addWidget(self.btnRightTwo)
        # </editor-fold>

    # 获取按键信号的发出者函数
    def f_getBtnSender(self):
        """
        function:  获取按键信号的发出者函数
              in:  None
             out:  None
          return:  None
          others:  Get Sender Of The Button Signal Func
        """
        if self.ui.titleBar.sender() is not None:
            return self.ui.titleBar.sender()
        elif self.ui.leftMenuBar.sender() is not None:
            return self.ui.leftMenuBar.sender()
        elif self.ui.leftColumn.sender() is not None:
            return self.ui.leftColumn.sender()

    # 在调整窗口大小时调整大小或更改位置
    def f_resizeGrips(self):
        """
        function:  在调整窗口大小时调整大小或更改位置
              in:  None
             out:  None
          return:  None
          others:  Resize Or Change Position When Resizing Window
        """
        if self.settings["custom_title_bar"]:
            self.left_grip.setGeometry(5, 10, 10, self.height())
            self.right_grip.setGeometry(self.width() - 15, 10, 10, self.height())
            self.top_grip.setGeometry(5, 5, self.width() - 10, 10)
            self.bottom_grip.setGeometry(5, self.height() - 15, self.width() - 10, 10)
            self.top_right_grip.setGeometry(self.width() - 20, 5, 15, 15)
            self.bottom_left_grip.setGeometry(5, self.height() - 20, 15, 15)
            self.bottom_right_grip.setGeometry(self.width() - 20, self.height() - 20, 15, 15)
